//
//  ThirdViewController.m
//  大众团购
//
//  Created by 安东 on 14-10-15.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ThirdViewController.h"
#import "ThirdMyShareViewController.h"
#import "ThirdScrollTableViewCell.h"
#import "ThirdButtonTableViewCell.h"
#import "UIScrollView+MJRefresh.h"
#import "MJRefreshBaseView.h"
#import "ThirdWeatherTableViewCell.h"

@interface ThirdViewController ()

@end

@implementation ThirdViewController

- (void)dealloc
{
    [_tableView release];
    
    
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"精品生活";
    
    self.optionIndices = [NSMutableIndexSet indexSetWithIndex:1];
    [self getButton];
    
    [self createTableView];
    
    
}

#pragma mark-
#pragma mark createTableView
- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 568) style:UITableViewStyleGrouped];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    //刷新
    [self setupRefresh];
    
}


#pragma mark-
#pragma mark tableView 数据源

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 || section == 1 || section == 2) {
        return 1;
    }
    return 5;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        ThirdScrollTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
        if (!cell) {
            cell = [[ThirdScrollTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        }
        
        for (int i = 0; i < 4; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(320 * i, 0, 320, 120);
            button.backgroundColor = [UIColor blackColor];
            button.tag = i;
            //[button setBackgroundImageWithURL:[_dataSource[i]  imgString] forState:UIControlStateNormal];
            
            [cell.scroll addSubview:button];
        }
        
        return cell;
    }
    if (indexPath.section == 1) {
        ThirdButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
        if (!cell) {
            cell = [[ThirdButtonTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        }
        for (int i = 0; i < 6; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
            button.frame = CGRectMake(10 * (i + 1) + 90 * i, 10, 90, 100);
            button.backgroundColor = [UIColor lightGrayColor];
            button.tag = i + 4;
            
            [cell.scroll addSubview:button];
        }
        
        return cell;

    }
    if (indexPath.section == 2) {
        ThirdWeatherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
        if (!cell) {
            cell = [[ThirdWeatherTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        }
        
        return cell;
    }
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 120;
    }
    if (indexPath.section == 1) {
        return 120;
    }
    
    return 90;
}


//分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 4;
}
// Default is 1 if not implemented

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        
        return @"今日特购";
    }
    if (section == 3) {
        return @"趣味生活";
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1 ||section == 3) {
        return 10;
    }
    
    return 0.1;
}

- (void)getButton
{
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
//    button.frame = CGRectMake(10, 10, 30, 30);
//    button.backgroundColor = [UIColor lightGrayColor];
//    [button setBackgroundImage:[UIImage imageNamed:@"burger@2x.png"] forState:UIControlStateNormal];
//    
//    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//    [self.navigationController.navigationBar addSubview:button];
    
    UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(10, 10, 100, 28)];
   
    
    //switchView.on = YES;//设置初始为ON的一边
   
    [switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
    [self.navigationController.navigationBar addSubview:switchView];
}


- (void)switchAction:(id)sender {
    NSArray *images = @[
                        [UIImage imageNamed:@"gear"],
                        [UIImage imageNamed:@"globe"],
                        [UIImage imageNamed:@"profile"],
                        [UIImage imageNamed:@"star"],
                        //                        [UIImage imageNamed:@"gear"],
                        //                        [UIImage imageNamed:@"globe"],
                        //                        [UIImage imageNamed:@"profile"],
                        //                        [UIImage imageNamed:@"star"],
                        //                        [UIImage imageNamed:@"gear"],
                        //                        [UIImage imageNamed:@"globe"],
                        //                        [UIImage imageNamed:@"profile"],
                        //                        [UIImage imageNamed:@"star"],
                        ];
    NSArray *colors = @[
                        [UIColor colorWithRed:240/255.f green:159/255.f blue:254/255.f alpha:1],
                        [UIColor colorWithRed:255/255.f green:137/255.f blue:167/255.f alpha:1],
                        [UIColor colorWithRed:126/255.f green:242/255.f blue:195/255.f alpha:1],
                        [UIColor colorWithRed:119/255.f green:152/255.f blue:255/255.f alpha:1],
                        //                        [UIColor colorWithRed:240/255.f green:159/255.f blue:254/255.f alpha:1],
                        //                        [UIColor colorWithRed:255/255.f green:137/255.f blue:167/255.f alpha:1],
                        //                        [UIColor colorWithRed:126/255.f green:242/255.f blue:195/255.f alpha:1],
                        //                        [UIColor colorWithRed:119/255.f green:152/255.f blue:255/255.f alpha:1],
                        //                        [UIColor colorWithRed:240/255.f green:159/255.f blue:254/255.f alpha:1],
                        //                        [UIColor colorWithRed:255/255.f green:137/255.f blue:167/255.f alpha:1],
                        //                        [UIColor colorWithRed:126/255.f green:242/255.f blue:195/255.f alpha:1],
                        //                        [UIColor colorWithRed:119/255.f green:152/255.f blue:255/255.f alpha:1],
                        ];
    
    RNFrostedSidebar *callout = [[RNFrostedSidebar alloc] initWithImages:images selectedIndices:self.optionIndices borderColors:colors];
    //    RNFrostedSidebar *callout = [[RNFrostedSidebar alloc] initWithImages:images];
    callout.delegate = self;
    //callout.showFromRight = YES;
    [callout show];
}

#pragma mark - RNFrostedSidebarDelegate

- (void)sidebar:(RNFrostedSidebar *)sidebar didTapItemAtIndex:(NSUInteger)index {
    //NSLog(@"Tapped item at index %i",index);
    if (index == 3) {
        [sidebar dismiss];
    }
    if (index == 2) {
        ThirdMyShareViewController *vc = [[ThirdMyShareViewController alloc] init];
        [self presentViewController:vc animated:YES completion:nil];
        [vc release];
    }
}

- (void)sidebar:(RNFrostedSidebar *)sidebar didEnable:(BOOL)itemEnabled itemAtIndex:(NSUInteger)index {
    if (itemEnabled) {
        [self.optionIndices addIndex:index];
    }
    else {
        [self.optionIndices removeIndex:index];
    }
}


#pragma mark -
#pragma mark 刷新
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    //[self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    [self.tableView footerEndRefreshing];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    self.tableView.headerPullToRefreshText = @"下拉可以刷新了";
    self.tableView.headerReleaseToRefreshText = @"松开马上刷新了";
    self.tableView.headerRefreshingText = @"正在刷新中";
    
    self.tableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    self.tableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    self.tableView.footerRefreshingText = @"加载中";
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    //下拉刷新
//    self.dataSource = [NSMutableArray array];
//    [self getGoodsData];
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        //_page += 10;
        
        
        //[_tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView headerEndRefreshing];
    });
}

- (void)footerRereshing
{
    //上拉加载
//    self.dataSource = [NSMutableArray array];
//    [self getGoodsData];
    
    
    //    [_tableView reloadData];
    
    
    // 2. 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.tableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView footerEndRefreshing];
    });
    //_page += 10;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
